package com.example.zs.mytest.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.zs.mytest.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zs
 * Date：2017年 06月 23日
 * Time：14:46
 * —————————————————————————————————————
 * About:
 * —————————————————————————————————————
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{


    private Context mContext;
    private List<String> dataList = new ArrayList<>();
    /**
     * handler
     **/
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:

                    break;
                case 1:

                    break;

                }
            super.handleMessage(msg);
        }
    };

    public MyAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_item_news_evaluate_grid,null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position, List<Object> payloads) {
//        super.onBindViewHolder(holder, position, payloads);
        if (payloads == null || payloads.size() < 1 ){
            onBindViewHolder(holder,position);
            Log.d("My_Log","==========");
        }else{
            Log.d("My_Log","11");
            if (position == mPosition){
                holder.text.setText("a" + payloads.get(0));
            }
        }

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        String title = dataList.get(position);
        holder.text.setText(title);
        Picasso.with(mContext).load("https://testwww.ibaodian.com/imgupload/information/despic/21501481510821167.jpg").into(holder.image);
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPosition = position;
                updateData();
            }
        });

    }

    int time;
    int mPosition;
    private void updateData(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    ++time;
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            notifyItemRangeChanged(0,getItemCount(),time);
                        }
                    });
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public void bindData(ArrayList<String> data){
        if (dataList != null && dataList.size() > 0) {
            dataList.clear();
        }
        dataList = (List<String>) data.clone();
        notifyDataSetChanged();

    }

    public void addData(ArrayList<String> data){

        if (data.size() > dataList.size()){
            for (int i = dataList.size(); i < data.size(); i++) {
                dataList.add(data.get(i));
            }
        }
        notifyDataSetChanged();

    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        private ImageView image;
        private TextView text;

        public MyViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.iv_news);
            text = (TextView) itemView.findViewById(R.id.titile_news);

        }
    }
}
